//获取用户信息
var user = "/index/api/getUser";
var paimin = "/index/api/getGameRank";
var img = $(".userHead");
var Text = $(".userName");

$.ajax({
	type: "post",
	url: user,
	async: true,
	success: function(res) {
		if(res.status != 1) {
			//判断是微信环境，则跳转授权
			if(navigator.userAgent.match(/MicroMessenger/)) {
				window.location.href = window.location.protocol + "//" + window.location.host + "/api/wechatoauth/login/?redirect_url=" + encodeURI(window.location.href);
			}
			//否则，则跳转普通登录
			else {
				window.location.href = window.location.protocol + "//" + window.location.host + "/api/weboauth/login/?redirect_url=" + encodeURI(window.location.href);
			}
		}
		img.attr("src", res.data.headimgurl)
		Text.text(res.data.username)
	}
});
$.ajax({
	type: "post",
	url: paimin,
	async: true,
	success: function(res) {
		//		console.log(res)
		$.each(res.data, function(index, val) {
			addLi(val.update_time, val.username, val.song_num, val.headimgurl)
		})
	}
});

function isFs(fsh) {
	var obj = {};
	if(fsh <= 5) {
		obj.dwImg = '../img/bronze.png';
		obj.gwStr = '青铜'
	} else if(fsh <= 10 && fsh >= 6) {
		obj.dwImg = '../img/silver.png';
		obj.gwStr = '白银';
	} else if(fsh <= 11 && fsh >= 15) {
		obj.dwImg = '../img/huangjing.png';
		obj.gwStr = '黄金';
	} else if(fsh > 16) {
		obj.dwImg = '../img/platinum.png';
		obj.gwStr = '铂金';
	}

	return obj;
}

function addLi(time, name, fsh, tx) {
	var old_li = $("<li>");
	$('<div class="rankHead"><img src="' + tx + '"></div>').appendTo(old_li);
	var duanwei = isFs(fsh);
	$('<div class="rankInfo clearfix"><div class="left fl"><p class="rankName">' + name + '</p><div><img class="gradePic" src="' + duanwei.dwImg + '"><span class="rankNum">' + duanwei.gwStr + '</span></div></div><div class="right fr"><p>猜中<span>' + fsh + '</span><span>首歌</span></p><p class="upTime">' + time + '</p></div></div>').appendTo(old_li);
	$(".list>ul").append(old_li)
}